package com.sharecode.stock.strategy;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.sharecode.stock.strategy.vo.Energy;
import com.sharecode.stock.strategy.vo.MACD;
import com.sharecode.stock.strategy.vo.StrategyVo;

/**
 * 背离策略
 */
public class MacdDeviateStrategy extends StrategySuper {
	private static final Map<String, Integer> noticeCountMap = new ConcurrentHashMap<>();

	public static void strategy(StrategyVo vo) {
		try {
			if (checkNoticeCount(vo, noticeCountMap)) {
				return;
			}
			List<Energy> energys = vo.getEnergys();
			if (energys.size() < 3) {
				return;
			}

			List<MACD> macd = vo.getMacd();

			int size = macd.size();
			int k1 = size - 1;
			int k2 = size - 2;
			int k3 = size - 3;
			Double e1 = macd.get(k1).getMacd();
			Double e2 = macd.get(k2).getMacd();
			Double e3 = macd.get(k3).getMacd();

			if (Math.abs(e1) >= Math.abs(e2)) {
				return;
			}
			Energy energy1 = energys.get(energys.size() - 1);
			Energy energy2 = energys.get(energys.size() - 2);
			Energy energy3 = energys.get(energys.size() - 3);

			if (energy2.getStockHistories().size() < 3) {
				return;
			}
			if (energy1.getStockHistories().size() > 8) {
				return;
			}

			if (e1 >= 0 && e2 >= 0) {// 0轴上方
				// 价格判断
				if (energy1.getMaxPrice() < energy3.getMaxPrice()) {
					return;
				}
				// 能量判断
				if (energy1.getMaxEnergy() > energy3.getMaxEnergy()) {
					return;
				}

//				if (e2 < e3) {
//					handle(vo, "背离-卖2");
//				} else {
//					handle(vo, "背离-卖1");
//				}
			} else if (e1 <= 0 && e2 <= 0) {// 0轴下方
				// 价格判断
				if (energy1.getMaxPrice() > energy3.getMaxPrice()) {
					return;
				}
				// 能量判断
				if (energy1.getMaxEnergy() < energy3.getMaxEnergy()) {
					return;
				}

				if (e2 > e3) {
					handle(vo, "背离-买2");
				} else {
					handle(vo, "背离-买1");
				}
			}
		} catch (Exception e) {
			logError(e);
		}
	}

	private static void handle(StrategyVo vo, String msg) {

		List<Energy> energys = vo.getEnergys();
		Energy energy1 = energys.get(energys.size() - 1);
		Energy energy3 = energys.get(energys.size() - 3);

		if (Math.abs(energy3.getMaxEnergy() / energy1.getMaxEnergy()) < 1.5) {
			return;
		}

		double ratio = Math.abs(energy1.getMaxPrice() - energy3.getMaxPrice()) / energy3.getMaxPrice();
		if (ratio > 0.015) {
			strategy(vo, msg, noticeCountMap,1);
		}
	}
}
